#include <iostream>
#include <algorithm>
using namespace std;

int n;	// 数字数目
int A[20]; // 无序数组

int max_production()
{
	// 若不要求时间复杂度为O(n)，则可以对整个数组排序
	std::sort(A, A + n);
	return max(A[0] * A[1] * A[n - 1], A[n - 1] * A[n - 2] * A[n - 3]);
}

int max_fast()
{
	// 要求O(n)时，只需要找到最大的3个数和最小的3个数
	int max_num[3], min_num[3];

	for (int i = 0; i < 3; i++)
	{
		for (int j = i + 1; j < n; j++)
			if (A[j] > A[i])
				swap(A[j], A[i]);
		max_num[i] = A[i];
	}
	for (int i = 0; i < 3; i++)
	{
		for (int j = i + 1; j < n; j++)
			if (A[j] < A[i])
				swap(A[j], A[i]);
		min_num[i] = A[i];
	}

	return max(max_num[0] * max_num[1] * max_num[2], max_num[0] * min_num[0] * min_num[1]);
}

int main()
{
	system("chcp 65001");

	cout << "请输入数字数目(n>=3)：";
	cin >> n;
	cout << "请输入" << n << "个数：" << endl;
	for (int i = 0; i < n; i++)
		cin >> A[i];

	cout << "最大乘积为：" << max_fast() << endl;
	return 0;
}